<?php
/**
 * @package qBase
 * @subpackage web
 */

/**
 * QWebContext provides static parameters to the current running context
 * 
 * @package qBase
 * @subpackage web
 */
final class QWebContext extends QObject 
{
	/**
	 * The default request that triggered the script execution
	 *
	 * @var QWebRequest
	 */
	private static $Request;
	/**
	 * The response that will be sent to the server
	 *
	 * @var QWebResponse
	 */
	private static $Response;
	/**
	 * The session associated to the browser that made the request
	 *
	 * @var QWebSession
	 */
	private static $Session;
	/**
	 * The call stack that has been requested by the client , the call stack is usually sent using GET
	 * and it is specified in this pattern : __qwl0QIWEBLISTENER=ClassName&__qwl1QIWEBLISTENER=ClassName&__qwl2QIWEBLISTENER=ClassName& and so on
	 * The call stack must be a list of strings that are the names of the classes to be called
	 *
	 * @var array
	 */
	public static $CallStack;
	/**
	 * While processing the request we increment the position in the stack
	 *
	 * @var integer
	 */
	public static $CallStackPosition = 0;
	
	/**
	 * Creates the current context based on the running PHP parameters
	 *
	 */
	public static function CreateContext()
	{
		// create web request
		self::$Request = new QWebRequest(true);
		self::$Response = new QWebResponse(true);
		self::$Session = new QWebSession();
		
		self::$Request->setResponse(self::$Response);
		
		self::$CallStack = array();

		
		$pos = 0;
		while (self::$Request->get("__qwl{$pos}"))
		{
			self::$CallStack[] = self::$Request->get("__qwl{$pos}");
			// self::$Request->unsetGet("__qwl{$pos}"); // unset was unstable
			$pos++;
		}
	}
	
	/**
	 * Gets the call stack
	 *
	 * @return array
	 */
	public static function GetCallStack()
	{
		return self::$CallStack;
	}
	
	/**
	 * Gets the position in the call stack
	 *
	 * @return array
	 */
	public static function GetCallStackPosition()
	{
		return self::$CallStackPosition;
	}
	/**
	 * Increments the call stack to the next position
	 *
	 * @return integer
	 */
	public static function IncrementCallStackPosition()
	{
		self::$CallStackPosition = self::$CallStackPosition + 1;
		return self::$CallStackPosition;
	}
	/**
	 * Gets the class name that is in the current call stack position
	 *
	 * @return string
	 */
	public static function GetCurrentCallStack()
	{
		if (self::$CallStack && isset(self::$CallStack[self::$CallStackPosition]))
			return self::$CallStack[self::$CallStackPosition];
		else 
			return null;
	}
	/**
	 * Gets The default request that triggered the script execution
	 *
	 * @return QWebRequest
	 */
	public static function Request()
	{
		return self::$Request;
	}
	/**
	 * Gets the response that will be sent to the browser
	 *
	 * @return QWebResponse
	 */
	public static function Response()
	{
		return self::$Response;
	}
	
	/**
	 * The session associated to the browser that made the request
	 *
	 * @return QWebSession
	 */
	public static function Session()
	{
		return self::$Session;
	}

	/**
	 * Gets the http host from the request
	 *
	 * @return string
	 */
	public static function GetHost()
	{
		return $_SERVER['HTTP_HOST'];
	}
	
	/**
	 * Gets the protocol of the request
	 *
	 * @return string
	 */
	public static function GetProtocol()
	{
		//HTTP/1.1
		$prot = explode("/", $_SERVER['SERVER_PROTOCOL']);
		return trim(strtolower($prot[0]));
	}
}


?>